/**
 * 通知设置
 * 
 * @param {}
 *            userId
 * @param {}
 *            subList
 * @return {}
 */
var setUserSubscriberWindow = function(userId, userName, subList) {
	// 记录勾选的记录id
	var recordIds = new Array();
	var userSubscriberForm = new Ext.form.FormPanel({
				anchor : '100% 15%',
				frame : true,
				region : "center",
				bodyBorder : false,
				labelAlign : 'left',
				monitorValid : true,
				items : [{
							layout : 'column',
							border : false,
							items : [{
										columnWidth : .50,
										layout : 'form',
										border : false,
										items : [{
													xtype : 'displayfield',
													anchor : '100%',
													ctCls : 'zhaospaceCSS3',
													fieldLabel : '用户编号',
													value : userId

												}]
									}, {
										columnWidth : .50,
										layout : 'form',
										border : false,
										items : [{
													xtype : 'displayfield',
													anchor : '100%',
													ctCls : 'zhaospaceCSS3',
													fieldLabel : '人员名称',
													value : userName
												}]
									}]
						}]
			});

	/**
	 * GridPanel
	 */
	var userSubscriberFormGridPanel = function() {
		var sm = new Ext.grid.CheckboxSelectionModel({
					listeners : {
						rowselect : {
							fn : function(e, rowIndex, record) {
								if (recordIds.length > 0) {
									for (var i = 0; i < recordIds.length; i++) {
										if (record.data["value"] == recordIds[i]) {
											return;
										}
									}
								}
								recordIds.unshift(record.data["value"]);
							}
						},
						rowdeselect : {
							fn : function(e, rowIndex, record) {
								for (var i = 0; i < recordIds.length; i++) {
									if (record.data["value"] == recordIds[i]) {
										recordIds.splice(i, 1);
										return;
									}
								}
							}
						}
					}
				});
		var userSubscriberColumn = new Ext.grid.ColumnModel({
					columns : [sm, new Ext.grid.RowNumberer(), {
								header : "通知编号",
								dataIndex : "value",
								width : 60
							}, {
								header : "通知类型名称",
								width : 100,
								dataIndex : "keyes"
							}]
				});
		var userSubscriberJson = new Ext.data.JsonReader({
					root : "list",
					fields : [{
								name : "value"
							}, {
								name : "keyes"
							}]
				});
		var userSubscriberStore = new Ext.data.Store({
			autoLoad : true,
			baseParams : {
				dictionaryType : PmsUtil.nofiti_message,
				existall : false,
				useable : true
			},
			proxy : new Ext.data.HttpProxy({
						url : __ctxPath
								+ '/systemDataDictionary/selectDataDictionaryById'
					}),
			listeners : {
				load : function(store) {
					if (recordIds.length == 0) {
						for (var j = 0; j < subList.length; j++) {
							recordIds.unshift(subList[j].subscriberTypeId
									.toString());
						}
					}
					if (recordIds.length == 0) {
						for (var j = 0; j < subList.length; j++) {
							recordIds.unshift(subList[j].subscriberTypeId
									.toString());
						}
					}
					store.each(function(record) {
						Ext.each(recordIds, function(item, index, allItems) {
									if (record.data["value"] == item) {
										sm.selectRecords([record], true);
										return false;
									}
								}, this);
					});
				}
			},
			reader : userSubscriberJson
		});
		var userSubscriberGrid = new Ext.grid.GridPanel({
					id : 'userSubscriberGrid',
					frame : true,
					region : "north",
					store : userSubscriberStore,
					cm : userSubscriberColumn,
					sm : sm,
					autoScroll : true,
					stripeRows : true,
					columnLines : true,
					loadMask : true,
					viewConfig : {
						forceFit : true
					},
					anchor : '100% 85%'
				});
		return userSubscriberGrid;
	}

	var userSubscriberWin = new Ext.Window({
		title : '通知类型设置',
		id : 'userSubscriberWin',
		width : 550,
		height : 320,
		closable : true,
		plain : true,
		modal : true,
		autoDestroy : true,
		closeAction : 'close',
		border : false,
		layout : 'anchor',
		items : [userSubscriberForm, userSubscriberFormGridPanel()],
		listeners : {
			'beforeshow' : function() {
				userSubscriberWin.center();
			}
		},
		buttonAlign : "center",
		buttons : [{
			text : "保存",
			scope : "true",
			handler : function() {
				Ext.Msg.confirm("信息确认", "您确认要提交吗?", function(btn, text) {
					if (btn == 'yes') {
						Ext.Ajax.request({
									url : __ctxPath
											+ "/systemAdmin/addUserSubscript",
									method : "POST",
									params : {
										userId : userId,
										subscript : recordIds.toString()
									},
									success : function(response, options) {
										var jsonData = Ext.util.JSON
												.decode(response.responseText);
										if (jsonData.success) {
											Ext.MessageBox.show({
														title : "信息提示",
														msg : jsonData.data,
														buttons : Ext.MessageBox.OK,
														icon : Ext.MessageBox.INFO
													});
											userSubscriberWin.close();
										} else {
											Ext.MessageBox.show({
														title : "错误提示",
														msg : jsonData.data,
														buttons : Ext.MessageBox.OK,
														icon : Ext.MessageBox.ERROR
													});
										}
									},
									failure : function(response, options) {
										Ext.MessageBox.show({
													title : "错误提示",
													msg : "设置提醒失败!",
													buttons : Ext.MessageBox.OK,
													icon : Ext.MessageBox.ERROR
												});
									},
									scope : this
								});
					}
				});
			}
		}, {
			text : "关闭",
			handler : function() {
				userSubscriberWin.close();
			}
		}]
	})
	return userSubscriberWin;
};